/*!
 * jQuery cxScroll 1.2.1
 * http://code.ciaoca.com/
 * https://github.com/ciaoca/cxScroll
 * E-mail: ciaoca@gmail.com
 * Released under the MIT license
 * Date: 2014-03-10
 */
import $ from './libs/jquery';
var cxScroll= $.fn.cxScroll = function (settings) {
    if (!this.length) {
        return
    }
    ;
    settings = $.extend({}, $.cxScroll.defaults, settings);

    var obj = this;
    var scroller = {
        lock: false,
        dom: {}
    };

    scroller.init = function () {
        scroller.dom.box = obj.find(settings.lableBoxCont);
        scroller.dom.list = scroller.dom.box.find(settings.index_tag);
        scroller.dom.items = scroller.dom.list.find(settings.eleChild);
        console.log(scroller.dom.items)
        scroller.itemSum = scroller.dom.items.length;




        scroller.dom.prevBtn = obj.find("."+settings.prev);
        if(scroller.dom.box.scrollLeft()<=0)
            scroller.dom.prevBtn.hide()

        scroller.dom.nextBtn = obj.find("."+settings.next);
        if(scroller.itemSum<=settings.eleNumber&&scroller.dom.box.scrollLeft()==0){
            scroller.dom.prevBtn.hide()
            scroller.dom.nextBtn.hide()
        }
        if(scroller.itemSum>settings.eleNumber&&scroller.dom.box.scrollLeft()==0){


            scroller.dom.nextBtn.show()
        }
        if(scroller.dom.nextBtn.parents('div').hasClass(settings.scrollPic4)){
            scroller.itemWidth = scroller.dom.items.outerWidth()+settings.margin-2;
        }
        if(!scroller.dom.nextBtn.parents('div').hasClass(settings.scrollPic4)){


            scroller.itemWidth = parseInt(scroller.dom.items.outerWidth(true));

        }


        if(scroller.itemSum>settings.eleNumber&&scroller.dom.box.scrollLeft()==(scroller.itemSum-settings.eleNumber)*scroller.itemWidth){
            scroller.dom.nextBtn.hide()
            scroller.dom.prevBtn.show()
        }
        scroller.itemHeight = scroller.dom.items.outerHeight();
        if (settings.direction == "left" || settings.direction == "right") {
            // 容器宽度不足时，不进行滚动
            if (scroller.itemWidth * scroller.itemSum <= scroller.dom.box.outerWidth()) {

                return
            }
            ;

            scroller.prevVal = "left";
            scroller.nextVal = "right";
            scroller.moveVal = scroller.itemWidth;
        } else {
            // 容器高度不足时，不进行滚动
            if (scroller.itemHeight * scroller.itemSum <= scroller.dom.box.outerHeight()) {

                return
            }
            ;

            scroller.prevVal = "top";
            scroller.nextVal = "bottom";
            scroller.moveVal = scroller.itemHeight;
        }
        ;

        // 元素：后补
        //scroller.dom.list.append(scroller.dom.list.html());

        // 添加元素：手动操作按钮
        if (settings.prevBtn && !scroller.dom.prevBtn.length) {

            scroller.dom.prevBtn = $("<a></a>", {"class": settings.prev}).prependTo(obj);

        }
        ;
        if (settings.nextBtn && !scroller.dom.nextBtn.length) {

            scroller.dom.nextBtn = $("<a></a>", {"class": settings.next}).prependTo(obj);
        }
        ;

        // 事件：鼠标移入停止，移出开始
        if (settings.auto) {
            obj.hover(function () {
                settings.auto = false;
                scroller.lock = false;
                scroller.off();
            }, function () {
                settings.auto = true;
                scroller.lock = false;
                scroller.on();
            });
        }
        ;

        scroller.bindEvents();
        scroller.on();
    };

    scroller.bindEvents = function () {

        if (settings.nextBtn && scroller.dom.prevBtn.length) {

            scroller.dom.nextBtn.bind("click", function () {
                window.flag=true;

                setTimeout(function(){
                    window.flag=false;
                },500)
                setTimeout(function(){
                    if (!scroller.lock) {
                        scroller.nextVal = "left";
                        scroller.goto(scroller.nextVal, settings.accel);

                    }
                },300)


            });



        }
        ;
        if (settings.prevBtn && scroller.dom.prevBtn.length) {
            scroller.dom.prevBtn.bind("click", function () {
                window.flag=true;
                setTimeout(function() {
                    window.flag=false;
                },500)
                setTimeout(function() {
                    if (!scroller.lock) {
                        scroller.nextVal = 'right';
                        scroller.goto(scroller.nextVal, settings.accel);

                    }
                    ;
                },300)

            });
        }
        ;
    };

    // 方法：开始
    scroller.on = function () {
        if (!settings.auto) {
            return
        }
        ;
        if (typeof(scroller.run) !== "undefined") {
            clearTimeout(scroller.run);
        }
        ;

        scroller.run = setTimeout(function () {
            scroller.goto(settings.direction);
        }, settings.time);
    };

    // 方法：停止
    scroller.off = function () {
        scroller.dom.box.stop(true);
        if (typeof(scroller.run) !== "undefined") {
            clearTimeout(scroller.run);
        }
        ;
    };

    // 方法：滚动
    scroller.goto = function (d, t) {
        scroller.off();
        if (settings.controlLock) {
            scroller.lock = true;
        }
        ;

        var _max;	// _max	滚动的最大限度
        var _dis;	// _dis	滚动的距离
        var _speed =settings.accel||settings.speed;

        switch (d) {
            case "right":
            case "top":
                _max = 0;
                if (d == "right") {
                    scroller.dom.prevBtn.css('display','block');

                    _dis = parseInt(scroller.dom.box.scrollLeft())-parseInt(scroller.moveVal * settings.step);

                    scroller.nextVal = "right";
                    scroller.dom.box.animate({"scrollLeft": _dis}, _speed, settings.easing, function () {
                        if(_dis <= _max)
                        {
                            _dis = _max;


                            scroller.dom.prevBtn.hide();

                            if(parseInt(scroller.itemSum)<=settings.eleNumber){

                                scroller.dom.nextBtn.hide();
                            }
                            else{
                                scroller.dom.nextBtn.show();
                            }


                        }
                        else{


                            if(parseInt(scroller.itemSum)<=settings.eleNumber){

                                scroller.dom.nextBtn.hide();
                            }
                            else{
                                scroller.dom.nextBtn.show();
                            }

                        }

                    });



                } else {
                    if (parseInt(scroller.dom.box.scrollTop(), 10) == 0) {
                        scroller.dom.box.scrollTop(scroller.itemSum * scroller.moveVal);
                    }
                    ;
                    _dis = scroller.dom.box.scrollTop() - (scroller.moveVal * settings.step);
                    if (_dis % scroller.itemHeight > 0) {
                        _dis -= (_dis % scroller.itemHeight) - scroller.itemHeight;
                    }
                    ;
                    if (_dis < _max) {
                        _dis = _max
                    }
                    ;
                    scroller.dom.box.animate({"scrollTop": _dis}, _speed, settings.easing, function () {
                        if (parseInt(scroller.dom.box.scrollTop(), 10) <= _max) {
                            scroller.dom.box.scrollTop(0);
                        }
                        ;
                    });
                }
                ;
                break;

            case "left":
            case "bottom":
                _max = (scroller.itemSum-settings.eleNumber) * (scroller.moveVal);


                if (d == "left") {

                    _dis = parseInt(scroller.dom.box.scrollLeft()) + parseInt((scroller.moveVal * settings.step));

                    _dis=_dis>=_max?_max:_dis;
                    scroller.dom.nextBtn.show();

                    scroller.nextVal = "left";
                    scroller.dom.box.animate({"scrollLeft": _dis}, _speed,function () {
                        if (parseInt(_dis)>=(parseInt(_max)-10)) {
                            _dis=_max;

                            scroller.dom.prevBtn.css('display','block');

                            scroller.dom.nextBtn.hide();
                        }
                        else if(scroller.itemSum<=settings.eleNumber&&scroller.dom.box.scrollLeft()==0){
                            scroller.dom.prevBtn.hide();


                            scroller.dom.nextBtn.hide();
                        }

                        else
                        {


                            scroller.nextVal = "left";
                            scroller.dom.prevBtn.css('display','block');
                            scroller.dom.nextBtn.show();
                        }
                        ;
                    });





                } else {
                    _dis = scroller.dom.box.scrollTop() + (scroller.moveVal * settings.step);
                    if (_dis % scroller.itemHeight > 0) {
                        _dis -= (_dis % scroller.itemHeight);
                    }
                    ;
                    if (_dis > _max) {
                        _dis = _max
                    }
                    ;
                    scroller.dom.box.animate({"scrollTop": _dis}, _speed, settings.easing, function () {
                        if (parseInt(scroller.dom.box.scrollTop(), 10) >= _max) {
                            scroller.dom.box.scrollTop(0);
                        }
                        ;
                    });
                }
                ;
                break;

            // not default
        }
        ;

        scroller.dom.box.queue(function () {
            if (settings.controlLock) {
                scroller.lock = false;
            }
            ;
            scroller.on();
            $(this).dequeue();
        });
    };

    scroller.init();
    return scroller;
};
// 默认值
$.cxScroll = {
    defaults: {
        direction: "left",	// 滚动方向
        easing: "swing",		// 缓动方式
        step: 3,				// 滚动步长
        accel: 10,			// 手动滚动速度
        speed: 10,			// 自动滚动速度
        time: 4000,			// 自动滚动间隔时间
        auto: false,			// 是否自动滚动
        prevBtn: true,		// 是否使用 prev 按钮
        nextBtn: true,		// 是否使用 next 按钮
        safeLock: true,		// 滚动时是否锁定控制按钮
        margin:12,
        prev:'prev',
        prevEnd:'prev_end',
        nextEnd:'next_end',
        next:'next',
        eleNumber:40,
        isShow:true,
        scrollPic4:'scrollPic4'

    }
};


